var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var MSearch = require('../../common/mSearch/index.js');
var Pagination = require('../../common/pagination/index.js');
var Dropdown = require('../../common/dropdown/index.js');
var app = getApp();

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,
        winHei: 0
    },

    onLoad(options) {
        common.initApp({
            extSuc: () => {

                this._mSearch = new MSearch({
                    scope: 'mSearch',
                    page: this
                }).init({
                    placeholder: '请输入店铺名称',
                    onSearch: (v) => {
                        this._q = v;
                        this._pagination && this._pagination.flush();
                    },
                });

                this._getList();
            },
            loginSuc: () => {},
            sysInfoSuc: (sysInfo) => {
                this.setData({
                    winHei: app.getWinHei(),
                });
            },
        });
    },

    onPullDownRefresh() {
        this._pagination && this._pagination.flush(() => {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom() {
        this._pagination && this._pagination.loadNext();
    },

    /**
     * 获取店铺列表
     * @return {[type]} [description]
     */
    _getList() {
        this._pagination = new Pagination({
            page: this,
            scope: 'pagination'
        }).init({
            rendData: (page, cb) => {

                ajax.G({
                    url: '/market/Home/getShopList',
                    data: {
                        page,
                        title: this._q || '',
                        category_id: this._cateId || 0,
                        floor_id: this._floorId || 0
                    },
                    success: (res) => {
                        var {
                            cateList = [],
                                floorList = [],
                                shopList = [],
                        } = res.result;
                        var allList = this.data.shopList || [];

                        // shopList = shopList.map((item) => {
                        //     item.image_urls = item.image_urls.split('|');
                        //     return item;
                        // });

                        if (!this._dropdown && cateList.length > 0 && floorList.length > 0) {

                            cateList = cateList.map((item) => {
                                return {
                                    id: item.id,
                                    option: item.cate_name
                                };
                            });
                            floorList = floorList.map((item) => {
                                return {
                                    id: item.id,
                                    option: item.title
                                };
                            });

                            this._dropdown = new Dropdown({
                                page: this,
                                scope: 'dropdown'
                            }).init({
                                tabs: ['全部分类', '全部楼层'],
                                contents: [
                                    cateList,
                                    floorList
                                ],
                                selectOption: (index, item) => {
                                    if (index == 0) { //分类
                                        this._cateId = item.id;
                                    } else if (index == 1) { //楼层
                                        this._floorId = item.id;
                                    }
                                    this._getList();
                                }
                            });
                        }

                        this.setData({
                            dataLoaded: true,
                            shopList: page == 1 ? shopList : allList.concat(shopList)
                        });

                        cb && cb(shopList.length);
                    }
                });

            }
        });
    },

    _bodyTap() {
        this._dropdown && this._dropdown.hide();
    }

}));